package com.yipinsoft.modules.wechat.core.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ResourceBundle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;

/**
 * 微信菜单操作
 * @author liulin
 *
 */
@RestController
@RequestMapping("${wechatPath}/manager/menu")
public class WechatMenuController {

    @Autowired
    private WxMpService wxService;

    
    /**
     * 自定义菜单查询接口
     * 详情请见： https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141014&token=&lang=zh_CN
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public WxMpMenu menuGet() throws WxErrorException {
        return this.wxService.getMenuService().menuGet();
    }
    /**
     * 通过本地Json创建菜单
     * @param menu
     * @return
     * @throws WxErrorException
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String menuCreate() throws WxErrorException {
    	//在wechat.properties文件中的menuFile配置
		URL url = this.getClass().getResource("/" + ResourceBundle.getBundle("wechat").getString("menuFile"));
		//ResourceBundle.getBundle("wechat").getString("menuFile");
		String json = "";
		try {
			File f = new File(url.getPath());
			if (f.isFile() && f.exists()) {
				InputStreamReader read = new InputStreamReader(new FileInputStream(f), "utf8");
				BufferedReader reader = new BufferedReader(read);
				String line;
				while ((line = reader.readLine()) != null) {
					json += line + "\n";
				}
				read.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
        String aa = this.wxService.getMenuService().menuCreate(json);
        return "创建菜单成功！";
        //return this.wxService.getMenuService().menuCreate(json);
    }

    /**
     * 自定义菜单删除接口
     * 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141015&token=&lang=zh_CN
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public void menuDelete() throws WxErrorException {
        this.wxService.getMenuService().menuDelete();
    }

    /**
     * 删除个性化菜单接口
     * 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN
     * @param menuId 个性化菜单的menuid
     */
    @RequestMapping(value = "/delete/{menuId}", method = RequestMethod.GET)
    public void menuDelete(@PathVariable String menuId) throws WxErrorException {
        this.wxService.getMenuService().menuDelete(menuId);
    }

    /**
     * 测试个性化菜单匹配结果
     * 详情请见: http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
     * @param userid 可以是粉丝的OpenID，也可以是粉丝的微信号。
     */
    @RequestMapping(value = "/menuTryMatch/{userid}", method = RequestMethod.GET)
    public WxMenu menuTryMatch(@PathVariable String userid) throws WxErrorException {
        return this.wxService.getMenuService().menuTryMatch(userid);
    }
}
